﻿using ModuleBase.googoltech.GTS;
using ModuleBase;
using System.Diagnostics;
using Newtonsoft.Json;
using Prism.Ioc;

namespace FilmPastingCompleted
{
    public class FilmPastingCompleted : ModuleBase.ModuleBase
    {
        private IAxisCardService _axisCardService;
        private ModuleStateService _moduleStateService;
        private IContainerProvider containerProvider;
        bool outputs = false;

        private object _parameter1;
        public object Parameter1
        {
            get => _parameter1;
            set
            {
                if (_parameter1 != value)
                {
                    _parameter1 = value;
                    OnPropertyChanged(nameof(Parameter1));
                }
            }
        }

        private double _parameter2;
        public double Parameter2
        {
            get => _parameter2;
            set
            {
                if (_parameter2 != value)
                {
                    _parameter2 = value;
                    OnPropertyChanged(nameof(Parameter2));
                }
            }
        }
        public override Dictionary<string, object> GetProperties()
        {
            return new Dictionary<string, object>
            {
                { "轴号", Parameter1 },
                { "速度", Parameter2 },
            };
        }

        public override Dictionary<string, List<object>> PropertyOptions { get; } = new Dictionary<string, List<object>>
        {
            { "方向", new List<object> { "正向", "负向" } },
            { "轴号", new List<object>{ 1,2,3,4,5,6,7,8 } }
        };

        public FilmPastingCompleted(IAxisCardService axisCardService, ModuleStateService moduleStateService)
        {
            Name = "贴膜完成";
        }


        public override bool Execute()
        {
            Status = ModuleStatus.Executing; // 设置状态为执行中
            AxisParm _axisX = GetAxisByNumber((int)Parameter2);
            var placement = 0.0; //放版位
            var boardposition = 0.0;
            using var mdb = containerProvider.Resolve<MDbContext>();
            var _mParam = mdb.MParams.FirstOrDefault(x => x.Name == "Placement");
            if (_mParam != null)
            {
                var _point = JsonConvert.DeserializeObject<MPoint>(_mParam.Value);
                placement = _point.X;
            }
            _mParam = mdb.MParams.FirstOrDefault(x => x.Name == "Boardposition");
            if (_mParam != null)
            {
                var _point = JsonConvert.DeserializeObject<MPoint>(_mParam.Value);
                boardposition = _point.X;
            }
            try
            {
                int stepNum = 1;
                while (true)
                {
                    switch (stepNum)
                    {
                        case 1: // 等待两个输入均为true
                            //if ((bool)input["Input1"] && (bool)input["Input2"])
                            {
                                stepNum = 2;
                            }
                            break;
                        case 2: //X轴运行到放版位
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.AxisPosMove(ref _axisX, placement, (double)Parameter2);
                                stepNum = 3;
                            }
                            break;
                        case 3: //Y轴运行到吸嘴取膜位
                            if (!_moduleStateService.IsStopped)
                            {
                                if (GTSCard.AxisPosMoveCheckDone(_axisX))
                                {
                                    stepNum = 4;
                                }
                            }
                            else
                            {
                                GTSCard.AxisStop(_axisX, 0);
                                stepNum = 2;
                            }
                            break;
                        case 4: //告知载板到位

                            stepNum = 5;
                            break;
                        case 5: //等待PLC回复接板完成

                            stepNum = 6;
                            break;
                        case 6://吸嘴Z轴运行到拍照位
                            if (!_moduleStateService.IsStopped)
                            {
                                GTSCard.AxisPosMove(ref _axisX, boardposition, (double)Parameter2);
                                stepNum = 7;
                            }
                            break;
                        case 7:
                            if (!_moduleStateService.IsStopped)
                            {
                                if (GTSCard.AxisPosMoveCheckDone(_axisX))
                                {
                                    stepNum = 8;
                                }
                            }
                            else
                            {
                                GTSCard.AxisStop(_axisX, 0);
                                stepNum = 6;
                            }
                            break;
                        case 8:
                            //需要告知轨道流程可以出板
                            outputs = true;
                            return outputs;
                    }
                }
            }
            catch (Exception exp)
            {
                outputs = false;
            }
            return outputs;
        }

        public override async Task<bool> ExecuteAsync()
        {
            var result = await Task.Run(() => Execute());
            return result;
        }

        public override void Initialize()
        {

        }
        private AxisParm GetAxisByNumber(int axisNumber)
        {
            switch (axisNumber)
            {
                case 1: return _axisCardService.A1;
                case 2: return _axisCardService.A2;
                case 3: return _axisCardService.A3;
                case 4: return _axisCardService.A4;
                case 5: return _axisCardService.A5;
                case 6: return _axisCardService.A6;
                case 7: return _axisCardService.A7;
                case 8: return _axisCardService.A8;
                default: return _axisCardService.A1;
            }
        }
    }
}
